﻿using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.HumanAffairs.Leaves;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.HumanAffairs.Leaves
{
    public class LeaveRepository : DapperDbContextRepository, ILeaveRepository
    {
        public LeaveRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 离职办理——查询分页显示
        /// <summary>
        /// 离职办理——查询分页显示
        /// </summary>
        /// <param name="LeaveName"></param>
        /// <param name="DeptId"></param>
        /// <param name="PositionId"></param>
        /// <param name="EntryTime"></param>
        /// <param name="LastTime"></param>
        /// <param name="State"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<LeaveDto>> GetLeavePageShow(string LeaveName, int DeptId, int PositionId, DateTime? EntryTime,DateTime? LastTime, int State, int pageindex = 1, int pagesize = 3)
        {
            PageResponse<LeaveDto> response = new PageResponse<LeaveDto>();
            var sql = "select a.*,b.DeptName as BName,c.PositionName as CName,d.DictionaryName as DName from Leave a join Department b on a.DeptId=b.Id join Position c on a.PositionId=c.Id join Dictionary d on a.State=d.Id where a.IsDelete=0";
            if (!string.IsNullOrWhiteSpace(LeaveName))
            {
                sql += $" and a.LeaveName like '%" + LeaveName + "%'";
            }
            if (DeptId > 0)
            {
                sql += $" and a.DeptId='{DeptId}'";
            }
            if (PositionId > 0)
            {
                sql += $" and a.PositionId='{PositionId}'";
            }
            if (EntryTime!=null)
            {
                sql += $" and a.EntryTime ='{EntryTime}'";
            }
            if (LastTime != null)
            {
                sql += $" and a.LastTime ='{LastTime}'";
            }
            if (State > 0)
            {
                sql += $" and a.State='{State}'";
            }
            var list = await this.QueryAsync<LeaveDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $" order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<LeaveDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        #endregion

        #region 离职办理——逻辑删除
        /// <summary>
        /// 离职办理——逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteLeave(int id)
        {

            var sql = $"update Leave set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        #endregion

        #region 离职办理——批量删除
        /// <summary>
        /// 离职办理——批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteBatchLeave(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Leave set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        #endregion

        #region 离职办理——编辑
        /// <summary>
        /// 离职办理——编辑
        /// </summary>
        /// <param name="leaveDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateLeave(LeaveDto leaveDto)
        {
            var sql = $"update Leave set LeaveNumber='{leaveDto.LeaveNumber}',LeaveName='{leaveDto.LeaveName}',DeptId='{leaveDto.DeptId}',PositionId='{leaveDto.PositionId}',SuperiorManager='{leaveDto.SuperiorManager}',EntryTime='{leaveDto.EntryTime}',LastTime='{leaveDto.LastTime}',Cause='{leaveDto.Cause}',CreateTime='{leaveDto.CreateTime}',State='{leaveDto.State}',Approver='{leaveDto.Approver}',IsDelete='{leaveDto.IsDelete}' where Id='{leaveDto.Id}'";
            return await ExecuteAsync(sql, leaveDto);
        }
        #endregion

        #region 离职办理——添加
        /// <summary>
        /// 离职办理——添加
        /// </summary>
        /// <param name="leaveDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddLeave(LeaveDto leaveDto)
        {
            var worker = new IdWorker(1, 1);
            var id = worker.NextId().ToString();
            var sql = $"insert into Leave values('{id}','{leaveDto.LeaveName}','{leaveDto.DeptId}','{leaveDto.PositionId}','{leaveDto.SuperiorManager}',getdate(),'{leaveDto.LastTime}','{leaveDto.Cause}',getdate(),'{leaveDto.State}','{leaveDto.Approver}',0)";
            return await ExecuteAsync(sql, leaveDto);
        }
        #endregion
    }
}
